<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('新增房间')" />
	<th:block th:include="include :: ztree-css" />
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-room-add">
			<input name="deptId" type="hidden" id="treeId"/>
			<div class="form-group">
				<label class="col-sm-3 control-label">归属部门：</label>
				<div class="col-sm-8">
					<div class="input-group">
						<input name="deptName" onclick="selectDeptTree()" id="treeName" type="text" placeholder="请选择归属部门" class="form-control" required>
						<span class="input-group-addon" onclick="selectDeptTree()"><i class="fa fa-search"></i></span>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">房间名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="roomName" id="roomName" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-3 control-label">房间类型：</label>
				<div class="col-xs-8">
					<select id="roomType" name="roomType" class="form-control" required>
						<option th:each="roomType:${roomTypes}" th:value="${roomType.id}" th:text="${roomType.roomTypeName}" th:disabled="${roomType.status == '1'}"></option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">房间楼层：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="roomFloor" id="roomFloor" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">状态：</label>
				<div class="col-sm-8">
			        <label class="toggle-switch switch-solid">
                        <input type="checkbox" id="status" checked>
                        <span></span>
                    </label>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">备注：</label>
				<div class="col-sm-8">
					<input id="remark" name="remark" class="form-control" type="text">
				</div>
			</div>
		</form>
	</div>
	<th:block th:include="include :: footer" />
	<script type="text/javascript">
		$("#form-room-add").validate({
			rules:{
				onkeyup: false,
				roomName:{
					minlength: 2,
					maxlength: 20,
					remote: {
		                url: ctx + "hotel/room/checkRoomNameUnique",
		                type: "post",
		                dataType: "json",
		                data: {
							"deptId" : function() {
								return $.common.trim($("#treeId").val());
							},
		                	"roomName" : function() {
		                		return $.common.trim($("#roomName").val());
		                    }

		                }
		            }
				},
			},
			messages: {
		        "roomName": {
		            remote: "房间名称已经存在"
		        },
		    },
		    focusCleanup: true
		});
		
		$('input').on('ifChanged', function(obj){
			var type = $(this).val();
			var checked = obj.currentTarget.checked;
			if (type == 1) {
			    if (checked) {
			        $._tree.expandAll(true);
			    } else {
			        $._tree.expandAll(false);
			    }
			} else if (type == "2") {
			    if (checked) {
			        $._tree.checkAllNodes(true);
			    } else {
			        $._tree.checkAllNodes(false);
			    }
			} else if (type == "3") {
			    if (checked) {
			        $._tree.setting.check.chkboxType = { "Y": "ps", "N": "ps" };
			    } else {
			        $._tree.setting.check.chkboxType = { "Y": "", "N": "" };
			    }
			}
		})
		
		function submitHandler() {
	        if ($.validate.form()) {
	        	add();
	        }
	    }

		/* 用户管理-新增-选择部门树 */
		function selectDeptTree() {
			var treeId = $("#treeId").val();
			var deptId = $.common.isEmpty(treeId) ? "100" : $("#treeId").val();
			var url = ctx + "system/user/selectDeptTree/" + deptId;
			var options = {
				title: '选择部门',
				width: "380",
				url: url,
				callBack: doSubmit
			};
			$.modal.openOptions(options);
		}

		function doSubmit(index, layero){
			var body = $.modal.getChildFrame(index);
			$("#treeId").val(body.find('#treeId').val());
			$("#treeName").val(body.find('#treeName').val());
			$.modal.close(index);
		}
	
		function add() {
			debugger
			var deptId = $("input[name='deptId']").val();
			var roomName = $("input[name='roomName']").val();
			var roomTypeId = $("#roomType").val();
			debugger
			var roomFloor = $("input[name='roomFloor']").val();
			var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
			var remark = $("input[name='remark']").val();

			$.ajax({
				cache : true,
				type : "POST",
				url : ctx + "hotel/room/add",
				data : {
					"deptId": deptId,
					"roomName": roomName,
					"roomTypeId": roomTypeId,
					"roomFloor": roomFloor,
					"status": status,
					"remark": remark
				},
				async : false,
				error : function(request) {
					$.modal.alertError("系统错误");
				},
				success : function(data) {
					$.operate.successCallback(data);
				}
			});
		}
	</script>
</body>
</html>
